import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import basic_func.general_func as bf


def func(pct, allvals):
    absolute = int(np.round(pct/100.*np.sum(allvals)))
    return f"{pct:.1f}%\n({absolute:d} g)"

def plot(df,params):
    bf.zh_font(params)
    fig, ax = plt.subplots(figsize=(8, 4),
                           subplot_kw=dict(aspect="equal"))
    wedges, texts, autotexts\
            =ax.pie(df.value,
                    autopct=lambda pct: func(pct, df.value),
                    textprops=dict(color="w"),
                    colors = bf.color_selector(df,params))

    ax.legend(wedges, df.index,
              title=params['legend_title'],
              loc="center left",
              bbox_to_anchor=(1, 0, 0.5, 1))
    plt.setp(autotexts, size=8, weight="bold")
    ax.set_title(params['title'],
                 fontname = params['title_fontname'],
                 fontsize = params['title_fontsize'])
    plt.show()

if __name__=='__main__':
    df,params = bf.read_data('test_data/饼图.xlsx')
    plot(df,params)
    
